//
//  CheckCommentCell.swift
//  Employee
//
//  Created by swordjoy on 2017/6/28.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class CheckCommentCell: UITableViewCell {

  static let identifier = "CheckCommentCell.swordjoy"
  
  static func height(with comment: String) -> CGFloat {
    let height = comment.sj.calculateHeight(UIScreen.width - 46.5, font: GlobalConfig.system14Font)
    print(height)
    return 120 + height
  }
  
  var isFirst: Bool = false {
    didSet {
      if isFirst {
        containerView.snp.updateConstraints({ (make) in
          make.top.equalTo(10)
        })
      }
      else {
        containerView.snp.updateConstraints({ (make) in
          make.top.equalTo(5)
        })
      }
    }
  }
  
  var model: CheckPeopleState? {
    didSet {
      guard let temp = model else { return }
      if "Auditing" == temp.approverStatus {
        stateLabel.text = "审批中"
        if temp.isCheck {
          stateView.backgroundColor = GlobalConfig.BGSkyBlueColor
          stateLine.backgroundColor = GlobalConfig.BGSkyBlueColor
        }
        else {
          stateView.backgroundColor = GlobalConfig.BGLightGreyColor
          stateLine.backgroundColor = GlobalConfig.BGLightGreyColor
        }
        iconImageView.setImage(with: temp.approvalStaffAvatar, placeholder: GlobalConfig.DefaultHeadIconImage)
        nameLabel.text = temp.approvalName
      }
      else if "NotPassed" == temp.approverStatus {
        stateLabel.text = "未通过"
        stateView.backgroundColor = GlobalConfig.BGSkyBlueColor
        stateLine.backgroundColor = GlobalConfig.BGSkyBlueColor
        iconImageView.setImage(with: temp.approvalStaffAvatar, placeholder: GlobalConfig.DefaultHeadIconImage)
        nameLabel.text = temp.approvalName
        timeLabel.text = DateHelper.getFormatTime(timeInmillisecond: temp.approverTime)
        commentLabel.text = temp.approverExplain
      }
      else if "Finished" == temp.approverStatus {
        stateLabel.text = "已同意"
        if temp.isCheck {
          stateView.backgroundColor = GlobalConfig.BGSkyBlueColor
          stateLine.backgroundColor = GlobalConfig.BGSkyBlueColor
        }
        else {
          stateView.backgroundColor = GlobalConfig.BGLightBlueColor
          stateLine.backgroundColor = GlobalConfig.BGLightBlueColor
        }
        timeLabel.text = DateHelper.getFormatTime(timeInmillisecond: temp.approverTime)
        iconImageView.setImage(with: temp.approvalStaffAvatar, placeholder: GlobalConfig.DefaultHeadIconImage)
        nameLabel.text = temp.approvalName
        commentLabel.text = temp.approverExplain
      }
      else if "noNeed" == temp.approverStatus {
        stateView.backgroundColor = GlobalConfig.BGLightGreyColor
        stateLine.backgroundColor = GlobalConfig.BGLightGreyColor
        iconImageView.setImage(with: temp.approvalStaffAvatar, placeholder: GlobalConfig.DefaultHeadIconImage)
        nameLabel.text = temp.approvalName
      }
    }
  }

  fileprivate var stateLine: UIView!
  
  fileprivate var containerView: UIView!
  fileprivate var stateView: UIView!
  fileprivate var stateLabel: UILabel!
  fileprivate var timeLabel: UILabel!
  fileprivate var commentLabel: UILabel!
  fileprivate var iconImageView: UIImageView!
  fileprivate var nameLabel: UILabel!
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    selectionStyle = .none
    backgroundColor = GlobalConfig.BGWhiteColor
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

}

// MARK: - fileprivate

extension CheckCommentCell {
  fileprivate func create() {
    
    containerView = UIView()
    contentView.addSubview(containerView)
    containerView.backgroundColor = UIColor.white
    containerView.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.bottom.equalToSuperview()
      make.top.equalTo(5)
    }
    containerView.layer.cornerRadius = 2
    containerView.clipsToBounds = true
    
    stateLine = UIView()
    contentView.addSubview(stateLine)
    stateLine.snp.makeConstraints { (make) in
      make.width.equalTo(3)
      make.left.equalTo(23.5)
      make.top.equalToSuperview()
      make.bottom.equalTo(containerView.snp.top)
    }
    
    stateView = UIView()
    containerView.addSubview(stateView)
    stateView.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.top.equalTo(25)
      make.height.width.equalTo(11)
    }
    stateView.layer.cornerRadius = 5.5
    stateView.clipsToBounds = true
    
    stateLabel = UILabel()
    containerView.addSubview(stateLabel)
    stateLabel.font = GlobalConfig.system16Font
    // MARK: -
    stateLabel.textColor = GlobalConfig.TextDeepColor
    stateLabel.snp.makeConstraints { (make) in
      make.left.equalTo(stateView.snp.right).offset(11)
      make.centerY.equalTo(stateView)
    }
    
    timeLabel = UILabel()
    containerView.addSubview(timeLabel)
    timeLabel.font = GlobalConfig.system12Font
    // MARK: -
    timeLabel.textColor = GlobalConfig.TextLightColor
    timeLabel.snp.makeConstraints { (make) in
      make.right.equalTo(-15)
      make.centerY.equalTo(stateView)
    }
    
    commentLabel = UILabel()
    containerView.addSubview(commentLabel)
    commentLabel.font = GlobalConfig.system14Font
    // MARK: -
    commentLabel.numberOfLines = 0
    commentLabel.textColor = GlobalConfig.Text08DeepColor
    commentLabel.snp.makeConstraints { (make) in
      make.right.equalTo(-15)
      make.left.equalTo(31.5)
      make.top.equalTo(52)
    }
    
    iconImageView = UIImageView()
    containerView.addSubview(iconImageView)
    iconImageView.image = GlobalConfig.DefaultHeadIconImage
    iconImageView.snp.makeConstraints { (make) in
      make.left.equalTo(32.5)
      make.height.width.equalTo(25)
      make.bottom.equalTo(-15)
    }
    iconImageView.layer.cornerRadius = 12.5
    iconImageView.clipsToBounds = true
    
    nameLabel = UILabel()
    containerView.addSubview(nameLabel)
    nameLabel.font = GlobalConfig.system14Font
    // MARK: -
    nameLabel.textColor = GlobalConfig.TextDeepColor
    nameLabel.snp.makeConstraints { (make) in
      make.left.equalTo(iconImageView.snp.right).offset(10)
      make.centerY.equalTo(iconImageView)
      make.right.equalTo(-50)
    }
    
  }
}
